/*
 * 文件名称: AdminAction.java
 * 版权信息: Copyright 2005-2013 SKY-MOBI Inc. All right reserved.
 * ----------------------------------------------------------------------------------------------
 * 修改历史:
 * ----------------------------------------------------------------------------------------------
 * 修改原因: 新增
 * 修改人员: Allen.Hu
 * 修改日期: 2013-3-30
 * 修改内容: 
 */
package com.drzui.tickets.admin.web;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.bigmouth.framework.web.action.json.JsonActionSupport;

import com.drzui.tickets.admin.AdminSessionUtils;
import com.drzui.tickets.admin.entity.Admin;
import com.drzui.tickets.admin.service.AdminService;

@Namespace("/base")
@Action("index")
@Results({ @Result(name = JsonActionSupport.SUCCESS, location = "/WEB-INF/pages/login.vm"),
		@Result(name = AdminLoginAction.ResultName.LoginOut, location = "/base/admin.shtml", type="redirect"),
        @Result(name = AdminLoginAction.ResultName.LoginSuccess, location = "/base/index/index.shtml", type="redirect"),
        @Result(name = AdminLoginAction.ResultName.index, location = "/WEB-INF/pages/template.vm")})
public class AdminLoginAction extends JsonActionSupport {

    private static final long serialVersionUID = -1183981655415621414L;

    private static final Logger logger = Logger.getLogger(AdminLoginAction.class);

    private AdminService adminService;

    private Admin admin;

    private String errorMessage;
    
    private String executeUrl;

	@Override
    public String execute() throws Exception {
    	admin = AdminSessionUtils.getSession();
    	if(admin != null){
    		return ResultName.LoginSuccess;
    	} else {
    		return SUCCESS;
    	}
    }
	
	/**
     * 进入首页
     */
	public String index() {
		admin = AdminSessionUtils.getSession();
    	if(admin != null){
    		executeUrl = "/WEB-INF/pages/index/index.vm";
    		return ResultName.index;
    	} else {
    		return SUCCESS;
    	}
	}
	
	/**
	 * 登录方法
	 * @return
	 */
    public String login() {
        try {
            boolean flag = adminService.login(admin);
            if (flag) {
                return ResultName.LoginSuccess;
            }
        }
        catch (Exception e) {
            logger.error(e);
        }
        errorMessage = "管理员账户不存在或密码错误。";
        return SUCCESS;
    }
    
    /**
     * 退出方法
     * @return
     */
    public String loginOut(){
    	try{
    		AdminSessionUtils.removeSession();
    	}
    	catch (Exception e) {
            logger.error(e);
        }
    	return ResultName.LoginOut;
    }

    public interface ResultName {
        String LoginSuccess = "LoginSuccess";
        String index = "index";
        String LoginOut = "LoginOut";
    }
    
    public AdminService getAdminService() {
        return adminService;
    }

    public void setAdminService(AdminService adminService) {
        this.adminService = adminService;
    }

    public Admin getAdmin() {
        return admin;
    }

    public void setAdmin(Admin admin) {
        this.admin = admin;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }
    
    public String getExecuteUrl() {
  		return executeUrl;
  	}

  	public void setExecuteUrl(String executeUrl) {
  		this.executeUrl = executeUrl;
  	}

}
